package com.andy.nicefi.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

import sun.misc.BASE64Encoder;

public class StringUtil {
	
	public static String getMD5String(String str) {
		if (str == null || "".equals(str.trim())) {
			return "";
		}
		String result = "";
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		BASE64Encoder base64en = new BASE64Encoder();

		try {
			result = base64en.encode(md5.digest(str.getBytes("utf-8")));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
	public static String getRandomString() {
		String result = "";
		Random r = new Random();
		int i = 0;
		int c;
		while (i < 10) {
			c = r.nextInt(122);
			if ((c >= 48 && c <= 57) || (c >= 65 && c <= 90)
					|| (c >= 97 && c <= 122)) {
				result = result + (char) c;
				i++;
			}
		}
		return result;
	}

}
